<?php
namespace AuctionCore\Account\Back;


/**
* 后台帐户组 测试类
* @author       爱民
*/
class AuthTest extends \PHPUnit_Framework_TestCase
{

	private $group_id = null;
	private $rule_id = null;

	public function _testGroup(){
		$group_info = array();
		$group_info['title'] = 'test';
		$group_info['status'] = 1;
		$group_info['rules'] = '';
		$group_info['remark'] = '后台帐号分组测试';

		//创建后台用户组
		$b_group =	new \AuctionCore\Account\Back\Group();		 	
		$insertID = $b_group->create($group_info);
		$this->assertTrue(is_numeric($insertID));
		$this->group_id = $insertID;

		//更新组信息
		$b_group =	new \AuctionCore\Account\Back\Group();		  		 	
		$group_info = array("title"=>'test_update');
		$ok = $b_group->update($group_info,"id='".$insertID."'");
		$this->assertEquals(1,$ok);
		$b_group->enable($insertID); //启用组
		$b_group->disable($insertID); //禁用组

		//分配组权限;
		$b_group =	new \AuctionCore\Account\Back\Group();	  		 	
		$ok = $b_group->add_rule($insertID,35);
		$info = $b_group->info($insertID);
		$rules = explode(',',$info['rules']);		 
		$this->assertTrue(in_array(35, $rules));
		$ok = $b_group->add_rule($insertID,array(35,36,37,38));
		$info = $b_group->info($insertID);
		$rules = explode(',',$info['rules']);
		$this->assertTrue(in_array(35, $rules)&&in_array(37,$rules));
		$this->assertEquals(1,$ok);

		//移组权限
		$ok = $b_group->remove_rule($insertID,array(35,37));
		$info = $b_group->info($insertID);
		$rules = explode(',', $info['rules']);
		$this->assertTrue(!in_array(35, $rules)&&!in_array(37,$rules));
	}
	public function _clearGroup(){
		//删除后台用户组
		$b_group =	new \AuctionCore\Account\Back\Group();	 	
		$ok = $b_group->del($this->group_id);
		$this->assertEquals(1,$ok);
	}

	public function testGroup(){
		$this->_testGroup();
		$this->_clearGroup();
	}

	public function _testRule(){
		$rule_info = array();	
		$rule_info['name'] = 'owner_center';
		$rule_info['title'] = '评估师';
		$rule_info['type'] = '1';
		$rule_info['status'] = '0';
		$rule_info['condition'] = '';

		//创建后台用户规则
		$b_Rule =	new \AuctionCore\Account\Back\Rule();		 	
		$insertID = $b_Rule->create($rule_info);
		$this->assertTrue(is_numeric($insertID));
		$this->rule_id = $insertID;

		//更新规则信息
		$b_Rule =	new \AuctionCore\Account\Back\Rule();		  		 	
		$rule_info = array("title"=>'aaaaa');
		$ok = $b_Rule->update($rule_info,"id='".$insertID."'");
		$this->assertEquals(1,$ok);
		$b_Rule->enable($insertID); //启用规则
		$b_Rule->disable($insertID); //禁用规则
	}
	public function _clearRule(){
		//删除后台用户规则
		$b_Rule =	new \AuctionCore\Account\Back\Rule();		 	
		$ok = $b_Rule->del($this->rule_id);
		$this->assertEquals(1,$ok);
	}

	public function testRule(){
		$this->_testRule();
		$this->_clearRule();

	}



	public function testCheckEmpRule(){
		//检查权限
		$this->_testRule();
		$this->_testGroup();
		//组分配权限
		$b_group =	new \AuctionCore\Account\Back\Group();
		$ok = $b_group->add_rule($this->group_id,$this->rule_id);

		//创建前台用户	[用户名，密码,昵称,手机号]
		$user_info = array();		  
		$user_info['depart_id'] = 2;  
		$user_info['username'] = 'unit_test';   
		$user_info['passwd'] = 'cccccc';     
		$user_info['email'] = 'test@clcw.com.cn';      
		$user_info['real_name'] = '宋人';  
		$user_info['sex'] = 1;        
		$user_info['mobile'] = '13552528384';     
		$user_info['avatar'] = '/www/aaa/aaa.jpg';     
		$user_info['describe'] = 'desctexttexttext';   
		$user_info['status'] = 1;    
		$user_info['province_id'] = 22;
		$user_info['store'] = 5; 
		$user_id = \AuctionCore\Account\Back\Employee::create($user_info);

		//检查权限		
		$emp = new \AuctionCore\Account\Back\Employee('unit_test');
		$emp->set_group($this->group_id);         //添加用户属组
		$ok = $emp->check_auth("owner_center");  //检查权限
		$this->assertTrue($ok);

		$emp->exgraft_group($this->group_id);//移出组	
		\AuctionCore\Account\Back\Employee::remove('unit_test'); //删除前台用户

		$this->_clearGroup();
		$this->_clearRule();
	}

}
?>